import { StorageUtils } from "../../utils/StorageUtils"

// pages/index/history.ts
Page({

  /**
   * 页面的初始数据
   */
  data: {
    results: [] as Array<string>
  },
  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {
    const results = StorageUtils.getStorageSync('history').split('#');
    this.setData({
      results: results
    })
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {
    this.setData({
      results: []
    })
  },
  /**
   * 删除记录
   */
  onDeleteResult(e: any) {
    const index = e.target.dataset.index;
    const results = this.data.results;
    results.splice(index, 1);
    this.setData({results: results});
    let history = results.length > 0 ? results[0] : '';
    for(let i=1; i<results.length; ++i) {
      history = history.concat('#', results[i]);
    } 
    StorageUtils.addStorageSync('history', history);
  }
})